//
//  AVDRendersView.m
//  AVD_Demo
//
//  Created by 3tee on 2022/11/17.
//

#import "AVDRendersView.h"

@interface AVDRendersView ()

@property (nonatomic, strong)NSMutableArray *mrenders;

@end

@implementation AVDRendersView

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.mrenders = [NSMutableArray array];
        [self setupUI:3 row:3];
    }
    return self;
}

-(void)setupUI:(NSInteger)c1 row:(NSInteger)r1{
    NSInteger col = c1;
    NSInteger row = r1;
    NSInteger count = col*row;
    CGFloat w = self.frame.size.width / col;
    CGFloat h = self.frame.size.height/row;
    for (int i = 0; i < count; i++) {
        AVDVideoView *render = [[AVDVideoView alloc]init];
        render.frame = CGRectMake(i%col*w, i/col*h, w, h);
        render.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
        [self addSubview:render];
        [self.mrenders addObject:render];
    }
    
}

- (void)updateLayoutRender:(NSInteger)count{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof AVDVideoView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.hidden = YES;
    }];
    if(count == 1){
        [self relayoutRender:1 row:1];
    }else if (count == 2){
        [self relayoutRender:1 row:2];
        //[self setupUI:1 row:2];
    }else if(count < 5 && count >2){
        [self relayoutRender:2 row:2];
        //[self setupUI:2 row:2];
    }else{
        [self relayoutRender:3 row:3];
//        [self setupUI:3 row:3];
    }
}

- (void)relayoutRender:(NSInteger)c1 row:(NSInteger)r1{
    NSInteger col = c1;
    NSInteger row = r1;
    NSInteger count = col*row;
    CGFloat w = self.frame.size.width / col;
    CGFloat h = self.frame.size.height/row;
    for (int i = 0; i < count; i++) {
        __block AVDVideoView *temp;
        [self.mrenders enumerateObjectsUsingBlock:^(AVDVideoView  *render, NSUInteger idx, BOOL * _Nonnull stop) {
            if(render.getVideoId){
                temp = render;
                *stop = YES;
            }
        }];
        temp.hidden = NO;
        temp.frame = CGRectMake(i%col*w, i/col*h, w, h);
        NSLog(@"relayout render = %@",temp);
        temp.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
    }
}

- (AVDVideoView *)getFreeRender{
    __block AVDVideoView *temp;
    [self.mrenders enumerateObjectsUsingBlock:^(AVDVideoView  *render, NSUInteger idx, BOOL * _Nonnull stop) {
        if(!render.getVideoId){
            temp = render;
            *stop = YES;
        }
    }];
    return temp;
}

- (AVDVideoView *)getRenderWithDeviceId:(NSString *)deviceId{
    __block AVDVideoView *temp;
    [self.mrenders enumerateObjectsUsingBlock:^(AVDVideoView  *render, NSUInteger idx, BOOL * _Nonnull stop) {
        if([render.getVideoId isEqualToString:deviceId]){
            temp = render;
            *stop = YES;
        }
    }];
    return temp;
}

@end
